package net.wang.totakecodenotes.wechatofficaccounts.controller.weixin;

import net.wang.totakecodenotes.wechatofficaccounts.domain.response.ResponseBuilder;
import net.wang.totakecodenotes.wechatofficaccounts.weixin.Authorize;
import net.wang.totakecodenotes.wechatofficaccounts.weixin.WeiXinService;
import org.apache.log4j.Logger;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

/**
 *
 */
@RestController
@RequestMapping("weixin/main")
public class WeiXinController {

    private static final Logger logger = Logger.getLogger(WeiXinController.class);
    @Resource
    private WeiXinService weiXinService;

    @GetMapping(value = "/index")
    public void getWeiXinUserInfo(String callBack, HttpServletResponse response) {
        logger.info("获取用户code接口 \tcallBack = " + callBack);
        weiXinService.getCode(callBack, response);
    }

    @GetMapping(value = "/callBack")
    public Object callBack(String code) throws Exception {
        logger.info("获取用户信息接口 \tcode = " + code);
        final Authorize authorize = weiXinService.getAccessTokenAndOpenId(code);
        return ResponseBuilder.build(weiXinService.getUserInfo(authorize));

    }

    @GetMapping(value = "/jsConfig")
    public Object getJsConfig(String url) throws Exception {
        logger.info(url);
        return ResponseBuilder.build(weiXinService.getWeiXinJSConfig(url));
    }
}
